package com.evildog.workspace.dependencies.ding.pusher;

import cn.hutool.http.HttpUtil;
import com.evildog.workspace.common.util.JacksonUtil;
import com.evildog.workspace.dependencies.ding.bean.DingRobotMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;

@Slf4j
@RequiredArgsConstructor
public class DingRobotPusher {
    private final String accessToken;

    @Async
    public void sendRobotMessage(DingRobotMessage dingRobotMessage) {
        try {
            String urlFormat = "https://oapi.dingtalk.com/robot/send?access_token=%1$s";
            HttpUtil.post(String.format(urlFormat, accessToken), JacksonUtil.serializeObject(dingRobotMessage));
        } catch (Exception e) {
            // 这里只记录异常，不想影响正常业务的使用
            log.error("sendRobotMessage message", e);
            log.error("sendRobotMessage message,{},", dingRobotMessage);
        }
    }
}
